<template>
  <div class="login-container">
    <!-- nav 区域 -->
    <van-nav-bar title="登录" class="page-nav-bar">
      <van-icon slot="left" name="arrow-left" @click="$router.back()"/>
    </van-nav-bar>
    <!-- 表单区域 -->
    <van-form @submit="onSubmit" ref="formRef">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down v-if="isCountDownShow" :time="1000*60" format="ss s" @finish="isCountDownShow = false"/>
          <!-- @click.passive="onSendSms" -->
          <van-button v-else round class="send-sms-btn" size="small" type="default" native-type="button" @click="onSendSms"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import { login, sendSms } from '../api/user.js'
export default {
  name: 'LoginPage',
  components: {

  },
  data () {
    return {
      user: {
        mobile: '17316774866',
        code: '246810'
      },
      userFormRules: {
        mobile: [
          { required: true, message: '请输入手机号' },
          { pattern: /^1[3|5|7|8]\d{9}$/, message: '手机号码格式错误!' }],

        code: [{ required: true, message: '请输入验证码' },
          { pattern: /^\d{6}$/, message: '验证码格式错误!' }]
      },
      isCountDownShow: false
    }
  },
  created () {

  },
  methods: {
    ...mapMutations(['setUser']),
    async onSubmit () {
      // 预验证
      // 发请求
      /* try-catch 程序员自己处理报错信息(不让浏览器处理影响后续代码执行) */
      try {
        this.$toast.loading({
          message: '登录中...',
          forbidClick: true,
          duration: 0
        })
        const { data } = await login(this.user)
        this.setUser(data.data)
        this.$toast.success('登录成功!')
        this.$router.back()
      } catch (error) {
        console.error(error)
        if (error.response.status === 400) {
          this.$toast.fail('手机号或者验证码错误!')
        } else {
          this.$toast.fail('请稍后重试!')
        }
        // console.dir(error)
      }

      // 成功/失败处理
    },
    async onSendSms () {
      try {
        await this.$refs.formRef.validate('mobile')
        console.log('验证成功')
        this.isCountDownShow = true
        await sendSms(this.user.mobile)
        this.$toast('发送成功')
      } catch (error) {
        this.isCountDownShow = false
        if (error.response) {
          if (error.response.status === 429) {
            this.$toast('操作过于频繁!')
          } else {
            this.$toast('发送失败!')
          }
        } else {
          return console.log('验证失败')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }

  .send-sms-btn {
    width: 180px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }

  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
